import { Suspense } from 'react'
import {Switch,Route,Redirect,withRouter} from 'react-router-dom'
function RouterView(props){
    const {routes} = props
    
    return <Suspense fallback='加载中...'>
        <Switch>
            {
                routes.map((item,index)=>{
                    if(item.component){
                        return <Route key={index} exact={item.exact} path={item.path} render={
                            ()=>{
                                const userinfo = JSON.parse(localStorage.getItem('loginData')||'{}')
                                if(item.requeireLogin){
                                    if(!userinfo.token){
                                        return <Route key={index} exact={item.exact} path={item.path}>
                                            <Redirect to='/login'></Redirect>
                                        </Route>
                                    }
                                }
                                var Component = withRouter(item.component)
                                return <Component />  
                            }
                        } />
                    }else{
                        return <Route key={index} exact={item.exact} path={item.path}>
                            <Redirect to={item.redirect}/>
                        </Route>
                    }
                    /* const userinfo = JSON.parse(localStorage.getItem('loginData')||'{}')
                    if(!userinfo.token && item.requeireLogin){
                        return <Route key = {index} sxact={item.exact} path={item.path}>
                            <Redirect to='/login'>
                            </Redirect>
                        </Route>
                    }
                    if(item.component){
                        return <Route key={index} exact={item.exact} path={item.path} component={item.component}>
                            </Route>
                    }else{
                        return <Route key={index} exact={item.exact} path={item.path} component={item.component}>
                                <Redirect to={item.redirect}/>
                            </Route>
                    } */
                })
            }
        </Switch>
    </Suspense>
}
export default RouterView